<%@ page import="java.util.Calendar" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="ru.niir.goodfil.ui.StorekeeperOrdersBean" %>
<%@ page import="ru.niir.goodfil.db.Order" %>
<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@taglib prefix="a4j" uri="http://richfaces.org/a4j" %>
<%@taglib prefix="rich" uri="http://richfaces.org/rich" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Goodwill - Заказы</title>
    <link rel="stylesheet" href="resources/css/main.css"/>

<%--
    <script type="text/javascript" src="resources/js/fixedtableheader.js"></script>
--%>
    <script type="text/javascript">
        function PrintContent(documentId)
        {
            var DocumentContainer = document.getElementById(documentId);
            var WindowObject = window.open("", "PrintWindow",
                    "width=750,height=650,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes");
            var htmlToPrint = '' +
                    '<style type="text/css">' +
                    'table th, table td {' +
                    'border:1px solid #000;' +
                    'padding:0.5em;' +
                    '}' +
                    'input[type="button"] {' +
                    'display:none;' +
                    '}' +
                    '</style>';
            htmlToPrint += DocumentContainer.outerHTML;
            WindowObject.document.writeln(htmlToPrint);
            WindowObject.document.close();
            WindowObject.focus();
            WindowObject.print();
            WindowObject.close();
        }

        function openMOIPage() {
            window.open("main_order_info", "Общая информация", "width=300,height=250,resizable=no,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no");
        }
    </script>
    <style type="text/css">
        .rich-table-subheadercell-rotate {
            -webkit-transform: rotate(-90deg);
            -moz-transform: rotate(-90deg);
            -o-transform: rotate(-90deg);
            -ms-transform: rotate(-60deg);
            filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1);
            white-space: normal;
            height: 100px;
            width: 2px;
            align:right;
        }

        .rich-table-subheadercell {
            white-space: normal;
            height: 120px;
        }

        td{
            height: 20px;
        }

        th{
            height: 100px;
        }

        table{
            border-collapse: collapse;
        }
/*        @media print {
            #links, #buttons {display: none;}
            .radiobutton {display: none;}
            .rich-menu-item {display: none;}
        }*/
    </style>

</head>
<body>
<f:view>

    <a4j:form id="jsForm" prependId="false">
        <a4j:jsFunction name="storekeepOrder" action="#{StorekeeperOrdersBean.storekeepOrder}" limitToList="true">
            <a4j:actionparam assignTo="#{StorekeeperOrdersBean.storekeepOrder_orderId}"/>
        </a4j:jsFunction>
        <a4j:jsFunction name="refresh" action="#{StorekeeperOrdersBean.refresh}" limitToList="true"
                        oncomplete="document.location='sk1';"/>
        <a4j:jsFunction name="addOrderId" action="#{StorekeeperOrdersBean.addOrderId}" limitToList="true">
            <a4j:actionparam assignTo="#{StorekeeperOrdersBean.orderId}"/>
        </a4j:jsFunction>
    </a4j:form>

    <jsp:include page="parts/version.jsp"/>
    <jsp:include page="StorageMenu.jsp"/>
    <h:form>

        <h:selectOneRadio value="#{StorekeeperOrdersBean.deliveryType}" styleClass="radiobutton">
            <f:selectItems value="#{StorekeeperOrdersBean.deliveryTypes}"/>
            <a4j:support action="#{StorekeeperOrdersBean.refresh}"
                         event="onclick"
                         reRender="hForm"
                    />
        </h:selectOneRadio>
    </h:form>

    <h:panelGroup id="links" layout="block" styleClass="">
        <a4j:commandLink value="Обновить" onclick="refresh();" style="margin-top: 10px; margin-left: 10px; font-size: 1.5em;"/>
        <a4j:commandLink value="Печать" onclick="PrintContent('hForm');" style="margin-top: 10px; margin-left: 10px; font-size: 1.5em;" />
    </h:panelGroup>

    <h:form id="hForm" prependId="false">
    <jsp:include page="messages.jsp"/>
    <jsp:include page="status.jsp"/>
    <h:panelGroup id="buttons" layout="block" styleClass="" style="padding:5px;" >
                <h2 align="center">ЗАКАЗ на сборку (<h:outputText value="#{StorekeeperOrdersBean.deliveryTypeAsString}"/>) на  <%=new SimpleDateFormat("dd.MM.yyyy HH:mm").format(new Date())%></h2>
            <div style="display:table; width:100%; margin-bottom: 10px;" id="buttons">
                <div align="left" style="display:table-cell;">
                    <a4j:commandButton value="Суммировать объединенное"
                                       title="Выберите клиентов для объединения сборки"
                                       onclick="Richfaces.showModalPanel('sum')"
                                       rendered="#{SecurityBean.isRoleAdministrator || SecurityBean.isRoleStoreman}"
                                       reRender="modalForm"/>
                </div>
                <div align="center" style="margin-right: 80px;">
                    <a4j:commandButton value="Установить порядок сборки и блокировки"
                                       title="Сортировать заказы на сборку
    по выбранному порядку"
                                       action="#{StorekeeperOrdersBean.save}"
                                       rendered="#{SecurityBean.isRoleAdministrator}"
                                       oncomplete="refresh();"
                                       style="float: inherit;"/>
                    <a4j:commandButton value="Сбросить порядок сборки"
                                       title="Восстановить порядок сборки \"по умолчанию\""
                                       action="#{StorekeeperOrdersBean.updateOrdersForDefaultSorting}"
                                       rendered="#{SecurityBean.isRoleAdministrator}"
                                       oncomplete="refresh();"
                                       style="float: inherit; margin-left: 5px;"/>
                </div>
                <div align="right" style="display:table-cell;">
                    <a4j:commandButton value="Сохранить причины срыва"
                                       action="#{StorekeeperOrdersBean.save}"
                                       rendered="#{SecurityBean.isRoleAdministrator || SecurityBean.isRoleStoreman}"
                                       reRender="ordersForBuid"
                            />
                </div>
            </div>
        </h:panelGroup>
                <rich:dataTable
                        id="ordersForBuid"
                        value="#{StorekeeperOrdersBean.ordersFromBuildPool}"
                        var="order"
                        rowKeyVar="row"
                        styleClass="full"
                        cellpadding="0"
                        >

                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="№"/>
                        </f:facet>
                        <h:outputText value="#{row + 1}. "/>
                    </rich:column>

                    <rich:column rendered="#{(SecurityBean.isRoleAdministrator || SecurityBean.isRoleStoreman)&& StorekeeperOrdersBean.isCanCommitOrders}"
                            >
                        <f:facet name="header">
                            <h:outputText value="Заказы"/>
                        </f:facet>
                        <h:outputLink value="#" onclick="storekeepOrder(#{order.id})" disabled="#{SecurityBean.isRoleStoreman && order.assemblyBlock}">
                            <h:outputText value="#{SecurityBean.isRoleStoreman && order.assemblyBlock ? 'блокировка' : 'открыть'}"/>
                        </h:outputLink>
                    </rich:column>

                    <rich:column rendered="#{SecurityBean.isRoleAdministrator || SecurityBean.isRoleStoreman}"
                                 headerClass="rich-table-subheadercell-rotate"
                                 width="10"
                                 title="Объединить выбранные">
                        <f:facet name="header">
                            <h:outputText value="Объед. выбр."/>
                        </f:facet>
                        <div align="center"><h:selectBooleanCheckbox immediate="true"
                                                                     onchange="addOrderId(#{order.id})"/></div>

                    </rich:column>

                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Номер ИС"/>
                        </f:facet>
                        <h:outputText value="#{order.id}" style="#{order.deliveryDateIsInPast ? 'color:red':''}"/>
                        <h:outputText value="(#{order.packageCountMultipliesAsString})" title="Все позиции заказа кратны гр. таре" style="#{order.deliveryDateIsInPast ? 'color:red':''}" rendered="#{order.packageCountMultiplies}"/>
                    </rich:column>

                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Номер 1С"/>
                        </f:facet>
                        <h:outputText value="#{order.c1Num}" style="#{order.deliveryDateIsInPast ? 'color:red':''}"/>
                    </rich:column>

                    <rich:column rendered="#{!SecurityBean.isRoleStoreman}">
                        <f:facet name="header">
                            <h:outputText value="Дата создания"/>
                        </f:facet>
                        <h:outputText value="#{order.creationDateAsString}" style="#{order.deliveryDateIsInPast ? 'color:red':''}"/>
                        <h:outputText value=" Изменен: #{order.changeDateAsString}" style="#{order.deliveryDateIsInPast ? 'color:red':''}" rendered="#{!order.changeDateNull}"/>
                        <a4j:commandLink value=" Доп. заказ к #{order.mainOrderId}" style="#{order.deliveryDateIsInPast ? 'color:red':''}" rendered="#{order.additional}" oncomplete="openMOIPage();">
                            <f:setPropertyActionListener value="#{order.mainOrderId}" target="#{StorekeeperOrdersBean.mainOrderId}"/>
                        </a4j:commandLink>
                    </rich:column>

                    <rich:column sortable="true" sortBy="#{order.deliveryDate}">
                        <f:facet name="header">
                            <h:outputText value="Дата отгрузки"/>
                        </f:facet>
                        <h:outputText value="#{order.deliveryDateAsString}"  style="#{order.deliveryDateIsInPast ? 'color:red':''}"/>
                    </rich:column>

                    <rich:column >
                        <f:facet name="header">
                            <h:outputText value="Менеджер"/>
                        </f:facet>
                        <h:outputText value="#{order.manager.fio}" style="#{order.deliveryDateIsInPast ? 'color:red':''}"/>
                    </rich:column>

                    <rich:column filterBy="#{order.client.companyName}" filterEvent="onblur">
                        <f:facet name="header">
                            <h:outputText value="Клиент"/>
                        </f:facet>
                        <h:outputText value="#{order.client.companyName}" style="#{order.deliveryDateIsInPast ? 'color:red':''}"/>
                    </rich:column>

                    <rich:column headerClass="rich-table-subheadercell-rotate">
                        <f:facet name="header">
                            <h:outputText value="Порядок сборки"/>
                        </f:facet>
                        <div align="center">
                        <h:inputText id="sort"
                                     value="#{order.sequence}"
                                     style="width:30px"
                                     readonly="#{SecurityBean.isRoleManager || SecurityBean.isRoleStoreman || SecurityBean.isRoleClient}"
                                     converterMessage="Поле \"Порядок сборки\" должно быть числом">
                            <f:convertNumber integerOnly="true" type="number" />
                            </div>
                        </h:inputText>
                    </rich:column>

                    <rich:column headerClass="rich-table-subheadercell-rotate" rendered="#{SecurityBean.isRoleAdministrator}">
                        <f:facet name="header">
                            <h:outputText value="Блокировка"/>
                        </f:facet>
                        <div align="center"><h:selectBooleanCheckbox value="#{order.assemblyBlock}"
                                                                     immediate="true"
                                                                     /></div>
                    </rich:column>

                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Статус клиента"/>
                        </f:facet>
                        <h:outputText value="#{order.client.clientStateAsString}" style="#{order.deliveryDateIsInPast ? 'color:red':''}"/>
                    </rich:column>

                    <rich:column headerClass="rich-table-subheadercell" sortable="true" sortBy="#{order.sumAsString}">
                        <f:facet name="header">
                            <h:outputText value="Сумма заказа"/>
                        </f:facet>
                        <h:outputText value="#{order.sumAsString}" style="#{order.deliveryDateIsInPast ? 'color:red':''}"/>
                    </rich:column>

                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Тип доставки"/>
                        </f:facet>
                        <h:outputText value="#{order.deliveryTypeAsString}" style="#{order.deliveryDateIsInPast ? 'color:red':''}"/>
                    </rich:column>

                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Причины срыва"/>
                        </f:facet>
                        <h:selectOneMenu value="#{order.failureCauseId}" styleClass="f_s"
                                         style="width:142px"
                                         disabled="#{SecurityBean.isRoleManager || SecurityBean.isRoleClient || SecurityBean.isRoleKassa}">
                            <f:selectItems value="#{StorekeeperOrdersBean.failureCauseList}"/>
                        </h:selectOneMenu>
                    </rich:column>
                    <f:facet name="footer">
                        <rich:columnGroup>
                            <rich:column/>
                            <rich:column/>
                            <rich:column/>
                            <rich:column/>
                            <rich:column/>
                            <rich:column/>
                            <rich:column/>
                            <rich:column/>
                            <rich:column/>
                            <rich:column/>
                            <rich:column/>
                            <rich:column rendered="#{SecurityBean.isRoleAdministrator}"/>
                            <rich:column>
                                <h:outputText value="Итого: #{StorekeeperOrdersBean.ordersForBuildSum}"/>
                            </rich:column>
                            <rich:column/>
                            <rich:column/>
                        </rich:columnGroup>
                    </f:facet>
                </rich:dataTable>

    </h:form>

    <rich:modalPanel id="sum" height="300">
        <h:form id="modalForm" styleClass="scroll_div">
            <div id="modalPrint">
                <rich:dataTable id="itemsSumTable"
                                value="#{StorekeeperOrdersBean.orderItemsSum}"
                                var="orderitem"
                                rowKeyVar="row"
                                styleClass="full"
                                >
                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="№"/>
                        </f:facet>
                        <h:outputText value="#{row + 1}"/>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Код товара"/>
                        </f:facet>
                        <h:outputText value="#{orderitem.goodCode}"/>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Количество"/>
                        </f:facet>
                        <h:outputText value="#{orderitem.goodCount}"/>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">
                            <h:outputText value="Свободный остаток"/>
                        </f:facet>
                        <h:outputText value="#{orderitem.freeGoodCount}"/>
                    </rich:column>
                </rich:dataTable>
            </div>

        </h:form>
        <h:panelGrid columns="2" styleClass="full" columnClasses="butLeft,butRight" cellspacing="0" cellpadding="0">
            <a4j:commandButton value="Печать"
                               styleClass="btnsz"
                               onclick="PrintContent('modalPrint');"/>
            <a4j:commandButton value="Закрыть"
                               styleClass="btnsz"
                               onclick="refresh();Richfaces.hideModalPanel('sum');"
                               />
        </h:panelGrid>
    </rich:modalPanel>
</f:view>
</body>
</html>
